"use strict";

/**
 * @ngdoc service
 * @name jgappApp.TaskReviewService
 * @description
 * # TaskReviewService
 * Service in the jgappApp.
 */
angular.module("jgappApp").service("TaskReviewService", function ($http, Util) {
  // AngularJS will instantiate a singleton by calling "new" on this function
  var TaskReview = function (options) {
    this.result = {};
    this.getData(options);
  };
  TaskReview.prototype.getData = function (options) {
    $http
      .get(options.url, {
        params: options.param,
      })
      .then(
        function (data, status, headers, config) {
          data = data.data;
          if (data.logout) {
            Util.logout();
            return;
          }
          if (data.success) {
            // 补充taskid和operationId两个字段
            var param = {};
            param.taskId = data.result.task && data.result.task.id;
            param.currentObjectForm = data.result.currentObjectForm;
            // 因为样式问题需要将同意驳回与不同意和已阅按钮隔离开
            if (data.result.operations) {
              param.operations = data.result.operations.filter(function (item) {
                return !(item.name == "不同意");
              });

              param.moreOperations = data.result.operations.filter(function (
                item
              ) {
                return item.name == "不同意";
              });
              var isNotPass = param.moreOperations.find(function (op) {
                return op.name === "不同意";
              });
              if (isNotPass) {
                // 当前人审批则可以转交
                param.moreOperations.push(
                  { id: isNotPass.id, name: "已阅" },
                  {
                    id: 20,
                    name: "转交",
                  },
                  {
                    id: 30,
                    name: "回退",
                  }
                );
              }
            }
            // param.moreOperations = [
            //   {
            //     id: 1,
            //     name: "已阅"
            //   },
            //   {
            //     id: 3,
            //     name: "不同意"
            //   }
            // ]
            // 如果是并行审批或者是自定义流程审批的话operationid用上面的替换
            if (
              param.currentObjectForm == "syncAduit" ||
              param.currentObjectForm == "caduit"
            ) {
              // 并行审批需要固定的按钮，如果后台没有返回按钮就按照同意和驳回返回
              param.operations = [
                {
                  id: 1,
                  name: "同意",
                },
                {
                  id: 0,
                  name: "驳回",
                },
              ];
              param.moreOperations = [
                {
                  id: 3,
                  name: "不同意",
                },
                {
                  id: 4,
                  name: "已阅",
                },
                // 下面两个id是随便写的，为了标识，不作为参数
                {
                  id: 20,
                  name: "转交",
                },
                {
                  id: 30,
                  name: "回退",
                },
              ];
            }
            if (param.operations) {
              param.operations.forEach(function (item, index) {
                if (item.name == "同意") {
                  item.className = "submit-flow";
                } else if (item.name == "驳回") {
                  item.className = "reject-flow";
                }
              });
            }
            var key = data.result.formKey || data.result.currentObjectName;
            this.result = angular.extend(data.result[key] || {}, param);
            if (options.callback) {
              options.callback(this.result, key);
            }
          } else {
            Util.Toast(data.result, {
              callback: Util.goBack,
              time: 5000,
            });
          }
        }.bind(this)
      );
  };
  return TaskReview;
});
